package com.xy.biz.base.service.impl;

import com.xy.biz.base.domain.bo.ExpressBO;
import com.xy.biz.base.domain.query.ExpressQuery;
import com.xy.biz.base.domain.req.GetExpressByNameRequest;
import com.xy.biz.base.domain.req.GetExpressRequest;
import com.xy.biz.base.domain.resp.SfExpressPropertiesResponse;
import com.xy.biz.base.manager.ExpressManager;
import com.xy.biz.base.service.ExpressService;
import com.xy.lang.domain.Result;
import com.xy.tool.json.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * @author Greekn Zhang
 * @date 2021/8/5 14:58
 * @description
 */
@Service
public class ExpressServiceImpl implements ExpressService {
    @Autowired
    private ExpressManager expressManager;

    /* */

    /**
     * 查询快递
     * @param
     *//*
    @Override
    public Result<List<DeliveryTrackingProgressBO>> getExpress(GetExpressRequest request) throws Exception {
        WaybillServiceAdapter expressServiceAdapter = ExpressAdmin.getExpressServiceAdapter(request.getExpressName());
        return expressServiceAdapter.getExpressInfo(request);
    }*/
    @Override
    public Result<?> saveExpress(ExpressBO expressBO) {
        int affectedRows = expressManager.saveExpress(expressBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    /**
     * 根据快递名字获取快递配置信息
     *
     * @param request
     */
    @Override
    public Result<SfExpressPropertiesResponse> getByExpress(GetExpressByNameRequest request) {
        ExpressBO expressBO = expressManager.getByExpress(request.getExpressName());
        SfExpressPropertiesResponse parse = JsonUtils.parse(expressBO.getSetting(), SfExpressPropertiesResponse.class);
        return Result.success(parse);
    }

    @Override
    public Result<?> saveAllExpress(Collection<ExpressBO> collection) {
        int affectedRows = expressManager.saveAllExpress(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updateExpress(ExpressBO expressBO) {
        int affectedRows = expressManager.updateExpress(expressBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> deleteExpress(java.lang.Long expressId) {
        int affectedRows = expressManager.deleteExpress(expressId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public ExpressBO getExpress(java.lang.Long expressId) {
        return expressManager.getExpress(expressId);
    }

    @Override
    public List<ExpressBO> listExpressById(Collection<Long> collection) {
        return expressManager.listExpressById(collection);
    }

    @Override
    public List<ExpressBO> listExpress(ExpressQuery query) {
        return expressManager.listExpress(query);
    }

    @Override
    public int countExpress(ExpressQuery query) {
        return expressManager.countExpress(query);
    }
}
